package com.int_tech.power.service;

import com.int_tech.power.dao.PowerDao;
import com.int_tech.power.entity.Power;
import com.int_tech.user.dao.UserDao;
import com.int_tech.user.entity.User;
import com.int_tech.utils.CarEnum;
import com.int_tech.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2017/9/23.
 */
@Service
public class PowerService {
    @Autowired
    private PowerDao powerDao;
    @Autowired
    private UserDao userDao;

    /**
    * TODO 添加权限信息
    * @param power
    * @return Result
    * @author dmy
    */
   /* public Result SavePower(Power power){
        try {
                if (userDao.findOne(power.getUserId())==null){
                    return Result.build(201,"用户id不存在");
                }
                powerDao.save(power);
                return Result.ok();
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }*/

    /**
    * TODO 根据id逻辑删除权限信息
    * @param id
    * @return Result
    * @author dmy
    */
    public Result delePower(Integer id){
        try {
            Power power = powerDao.findOne(id);
            power.setId(CarEnum.OFF.getCode());
            powerDao.save(power);
            return Result.ok();
        } catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }
    /**
    * TODO 修改用户信息
    * @param power
    * @return Result
    * @author dmy
    */
   /* @Transactional
    public Result updatePower(Power power){
        try {
            if (userDao.findOne(power.getUserId())==null){
                return Result.build(201,"用户id不存在");
            }
            powerDao.save(power);
            return Result.ok();
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }*/
    /**
    * TODO 查询所有权限信息
    * @return Result(List<Power>)
    * @author dmy
    */
    public Result findAllPower(){
        try {
            return Result.ok(powerDao.findAllByIsDel(CarEnum.ON.getCode()));
        } catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }

    /**
    * TODO 根据权限id查询权限信息
    * @param id
    * @return Result
    * @author dmy
    */
    public Result findPowerById(Integer id){
        try {
        	return Result.ok(powerDao.findOne(id));
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }
    /**
    * TODO 根据权限名查询权限信息
    * @param name
    * @return Result
    * @author dmy
    */
    public Result findPowerByName(String name){
        try {
        		return Result.ok(powerDao.findPowerByNameAndIsDel(name,CarEnum.ON.getCode()));
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }
    /**
    * TODO 根据用户id查询权限信息
    * @param userId
    * @return Result
    * @author dmy
    */
    /*public Result findPowerByUserId(Integer userId){
        try {
        	return Result.ok(powerDao.findPowerByUserIdAndIsDel(userId,CarEnum.ON.getCode()));
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }*/
    /**
    * TODO 根据用户名查询用户信息
    * @param userName
    * @return Result
    * @author dmy
    */
    /*public Result findPowerByUserName(String userName){
        try {
        		return Result.ok(powerDao.findPowerByUserNameAndIsDel(userName,CarEnum.ON.getCode()));
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }*/

    /**
     * 通过事件以及用户id鉴权
     * @param action,userId
     * @param userId
     * @return
     */
    public Result checkPowerByUser(String action,Integer userId)
    {
        //action用于区分用户操作事件；
        //1_1前面的数字代表事件，后面的数据代表操作类型
        //前面数字，1-客户管理权限；2-用户管理权限；3-场站管理权限；4-车辆管理权限；5-设备管理权限；6-装载率报表查看权限（待定）
        //后面数字，0-增；1-查；2-改；3-删
        User user = userDao.findById(userId);

        if(user == null)
        {
            return Result.build(500,"用户不存在");
        }
        else
        {
            Power power = powerDao.findByType(user.getType());

            String isDoIt = "";

            switch (action.split("_")[0])
            {
                case "1" :
                    isDoIt = power.getClientMan();
                    break;
                case "2" :
                    isDoIt = power.getUserMan();
                    break;
                case "3" :
                    isDoIt = power.getStationMan();
                    break;
                case "4" :
                    isDoIt = power.getCarMan();
                    break;
                case "5" :
                    isDoIt = power.getSidMan();
                    break;
            }

            String canBeDone = isDoIt.split(",")[Integer.parseInt(action.split("_")[1])];

            if("Y".equals(canBeDone))
            {
                return Result.build(200,"doit");
            }
            else {
                return Result.build(500,"canNotBeDone");
            }

        }
    }


}
